import os
from time import sleep

import allure
import pytest

import page
from data.account_info import skip_wsy_online
from page.page_glsx import PageGlsx
from page.page_login import PageLogin
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("跟老师学页面")
class TestGlsxList1:
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.glsxlist = PageGlsx(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin(os.environ['GLSX_LIST_USERNAME'], password='123456')
        # cls.login.pagelogin(username='11100003366', password='123456')
        cls.glsxlist.pageglsx_click_glsx_tab()
        try:
            cls.glsxlist.pageglsx_click_close_tanchuang()
        except:
            pass

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @allure.story("青柠详情页测试用例")
    def test01_glsxlist_qingning(self):
        self.glsxlist.pageglsx_click_list_qingning()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_qingning_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '领取青柠课程跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("青柠列表按钮测试用例")
    def test02_glsxlist_qingning_list_btn(self):
        self.glsxlist.pageglsx_click_qingning_list_btn()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_qingning_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '领取青柠课程跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("付费课详情页测试用例")
    def test03_glsxlist_ffk(self):
        self.glsxlist.pageglsx_click_list_ffk()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_ffk_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '付费课跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("付费课列表按钮测试用例")
    def test04_glsxlist_ffk_list_btn(self):
        self.glsxlist.pageglsx_click_ffk_list_btn()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_ffk_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '付费课跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("网销课详情页测试用例")
    def test05_glsxlist_wxk(self):
        self.glsxlist.pageglsx_click_list_wxk()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_wxk_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '领取网销课程跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("网销课列表按钮测试用例")
    def test06_glsxlist_wxk_list_btn(self):
        self.glsxlist.pageglsx_click_wxk_list_btn()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_wxk_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '领取网销课程跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("英语付费课详情页测试用例")
    def test07_glsxlist_ffk_en(self):
        self.glsxlist.pageglsx_click_list_ffk_en()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_ffk_success_en()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '付费课跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("英语付费课列表按钮测试用例")
    def test08_glsxlist_ffk_list_btn_en(self):
        self.glsxlist.pageglsx_click_ffk_list_btn_en()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_ffk_success_en()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '付费课跳转失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

@allure.epic("跟老师学页面")
class TestGlsxList2:
    num = 0

    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.glsxlist = PageGlsx(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        try:
            cls.login.pagelogin_close_login()
        except:
            pass
        cls.login.pagelogin_close_first_page_tanchuang()
        cls.glsxlist.pageglsx_click_glsx_tab()
        try:
            cls.glsxlist.pageglsx_click_close_tanchuang()
        except:
            pass

    @classmethod
    def teardown_class(cls):
        cls.num = 0
        GetDriver.quit_app_driver()

    @classmethod
    def setup_method(cls):
        cls.login.pagelogin_click_mine()
        username = os.environ['GLSX_LIST_USERNAME_AUTO'] + str(cls.num)
        # username = '1110000216' + str(cls.num)
        cls.login.pagelogin(username, password='123456')
        if cls.num == 9:
            cls.num = 0
        else:
            cls.num += 1

    @classmethod
    def teardown_method(cls):
        cls.login.pagelogin_click_mine()
        cls.login.pagelogin_logout()
        cls.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("小程序直播课详情页测试用例")
    def test01_glsxlist_xcxzbk(self):
        self.glsxlist.pageglsx_click_glsx_tab()
        self.glsxlist.pageglsx_click_list_xcxzbk()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_xcxzbk_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '小程序直播课领取失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @pytest.mark.skipif(condition=bool(int(os.environ['SKIP_WSY_ONLINE'])), reason='跳过正式站测试开关打开，跳过正式站用例')
    @allure.story("小程序列表按钮测试用例")
    def test02_glsxlist_xcxzbk_list_btn(self):
        self.glsxlist.pageglsx_click_glsx_tab()
        self.glsxlist.pageglsx_click_xzxzbk_list_btn()
        self.glsxlist.pageglsx_return_to_glsxpage_keycode_no_swipe()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_xzxzbk_list_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '小程序直播课列表按钮领取失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("咨询课1详情页测试用例")
    def test03_glsxlist_zxk_1(self):
        self.glsxlist.pageglsx_click_glsx_tab()
        self.glsxlist.pageglsx_click_list_zxk_1()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_zxk_1_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '咨询课1详情页领取失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("咨询课1列表按钮测试用例")
    def test04_glsxlist_zxk_1_list_btn(self):
        self.glsxlist.pageglsx_click_glsx_tab()
        self.glsxlist.pageglsx_click_list_zxk_1_mflq_btn()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_zxk_1_list_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '咨询1列表按钮领取失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("咨询课2详情页测试用例")
    def test05_glsxlist_zxk_2(self):
        self.glsxlist.pageglsx_click_glsx_tab()
        self.glsxlist.pageglsx_click_list_zxk_2()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_zxk_2_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '咨询课2详情页领取失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()

    @allure.story("咨询课2列表按钮测试用例")
    def test06_glsxlist_zxk_2_list_btn(self):
        self.glsxlist.pageglsx_click_glsx_tab()
        self.glsxlist.pageglsx_click_list_zxk_2_mflq_btn()
        try:
            sleep(3)
            assert self.glsxlist.pageglsx_if_zxk_2_list_success()
            log.info('断言成功')
            self.glsxlist.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsxlist.base_get_screenshot_as_file(module_name, '咨询2列表按钮领取失败')
            log.error('断言失败')
            raise
        finally:
            self.glsxlist.pageglsx_return_to_glsxpage_keycode()